package com.app.newcsc.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.newcsc.R;
import com.app.newcsc.entity.ExchangeCoinEntity;
import com.app.newcsc.fragment.my.exchangepoints.ExchangePointsViewModel;
import com.app.newcsc.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: woking
 * @date: 2021/6/25
 * @description：
 */
public class ExchangeHomeRightAdapter extends BaseAdapter {
    private List<ExchangeCoinEntity.CoinExchange.ToCoin> mDatas = new ArrayList<>();
    private Context mContext = null;
    private List<ExchangeCoinEntity.CoinExchange> mMoneyList = new ArrayList<>();
    private ExchangePointsViewModel model;

    public ExchangeHomeRightAdapter(Context context, List<ExchangeCoinEntity.CoinExchange.ToCoin> datas, List<ExchangeCoinEntity.CoinExchange> moneyList) {
        mDatas = datas;
        mContext = context;
        mMoneyList = moneyList;
    }

    public ExchangeHomeRightAdapter(Context context, List<ExchangeCoinEntity.CoinExchange.ToCoin> datas, List<ExchangeCoinEntity.CoinExchange> moneyList, String type, ExchangePointsViewModel model) {
        mContext = context;
        mMoneyList = moneyList;
        this.model = model;
        if(datas!=null && datas.size() > 0)
        {
            if(type.equalsIgnoreCase("AD"))
            {
                mDatas.clear();
                for(ExchangeCoinEntity.CoinExchange.ToCoin coinExchange : datas)
                {
                    if(coinExchange.coin_name.equals("积分") || coinExchange.coin_name.equals("AD"))
                    {
                        mDatas.add(coinExchange);
                    }
                }
            }
            else if(type.equalsIgnoreCase("CSC"))
            {
                mDatas.clear();
                for(ExchangeCoinEntity.CoinExchange.ToCoin coinExchange : datas)
                {
//                    if(!coinExchange.coin_name.equals("积分"))
//                    {
                        mDatas.add(coinExchange);
//                    }
                }
            }
            else
            {
                mDatas = datas;
            }
        }
    }


    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return mDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if (view == null) {
            vh = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_exchangehome_list, null);
            vh.ad_img =  view.findViewById(R.id.ad_img);
            vh.tv_name = view.findViewById(R.id.tv_name);
            vh.tv_yue = view.findViewById(R.id.tv_yue);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        if(mDatas != null && i < mDatas.size())
        {
//        vh.id.setText(mDatas.get(i).id);
            vh.tv_name.setText(mDatas.get(i).coin_name);
            if (mDatas.get(i).coin_name.equals("积分")){
                vh.tv_yue.setText(model.mPointEntity.getValue().point);
            }else {
                vh.tv_yue.setText(getMoney(mDatas.get(i)));
            }
            GlideUtils.getInstances().loadRoundCornerImg(mContext,vh.ad_img,10,mDatas.get(i).image);
        }
        return view;
    }

    private String getMoney(ExchangeCoinEntity.CoinExchange.ToCoin toCoin)
    {
        for(int i = 0; i < mMoneyList.size(); i++)
        {
            if(mMoneyList.get(i).coin_name.equals(toCoin.coin_name))
            {
                return mMoneyList.get(i).money;
            }
        }
        return "";
    }


    public final class ViewHolder {
        public ImageView ad_img;
        public TextView tv_name;
        public TextView tv_yue;
    }
}